package logic;

public class Vertex {
	
	public double x;
	public double y;
	boolean used;
	public int index;
	Vertex[] topk;
	
	public Vertex(double x, double y,int index){
		this.x = x;
		this.y = y;
		used = false;
		this.index = index;
	}
	
	public int getEuclideanDistance(Vertex vertex){
		double deltax = this.x - vertex.x;
		double deltay = this.y - vertex.y;
		double distance = Math.sqrt(deltax*deltax + deltay*deltay);
		return (int)Math.round(distance);
	}
	
	public static boolean inTriangle(Vertex A, Vertex B, Vertex C, Vertex P){
		// Compute vectors     
		double[] v0 = {C.x - A.x, C.y - A.y};
		double[] v1 = {B.x - A.x, B.y - A.y};
		double[] v2 = {P.x - A.x, P.y - A.y};

		// Compute dot products
		double dot00 = v0[0]*v0[0]+v0[1]*v0[1];
		double dot01 = v0[0]*v1[0]+v0[1]*v1[1];
		double dot02 = v0[0]*v2[0]+v0[1]*v2[1];
		double dot11 = v1[0]*v1[0]+v1[1]*v1[1];
		double dot12 = v1[0]*v2[0]+v1[1]*v2[1];

		// Compute barycentric coordinates
		double invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
		double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
		double v = (dot00 * dot12 - dot01 * dot02) * invDenom;

		// Check if point is in triangle
		return (u >= 0) && (v >= 0) && (u + v < 1);
	}
	
	public String toString(){
		return "(" + x + "," + y + ")";
	}
	
	public Vertex clone(){
		return new Vertex(x, y, index);
	}
}
